﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Biz_Dal.Biz;

namespace Project_BookIdeas
{
    public partial class FormQLKhachHang : Form
    {
        public FormMain frmMain;
        BizKhachHang kh;
        public FormQLKhachHang()
        {
            InitializeComponent();
            groupBoxThongTin.Enabled = false;
        }

        private void FormQLKhachHang_Load(object sender, EventArgs e)
        {
            show_KH();
        }

        public void show_KH()
        {
            lsvKhachhang.Items.Clear();
            groupBoxThongTin.Enabled = false;
            kh = new BizKhachHang();
            List<BizKhachHang> list = BizKhachHang.getListKhachHangAll();
            foreach (BizKhachHang read in list)
            {

                    int i = lsvKhachhang.Items.Count;
                    lsvKhachhang.Items.Add(read.idUser.ToString());
                    lsvKhachhang.Items[i].SubItems.Add(read.hoTen);
                    lsvKhachhang.Items[i].SubItems.Add(read.tenDangNhap);
                    if (read.laQuanTri.ToString() == "True")
                    {
                        lsvKhachhang.Items[i].SubItems.Add("Admin");
                    }
                    else
                    {
                        lsvKhachhang.Items[i].SubItems.Add("");
                    }
                    if (read.laUserXau.ToString() == "True")
                    {
                        lsvKhachhang.Items[i].SubItems.Add("Bad");
                    }
                    else
                    {
                        lsvKhachhang.Items[i].SubItems.Add("Good");
                    }
                    lsvKhachhang.Items[i].SubItems.Add(read.diaChi);
                    lsvKhachhang.Items[i].SubItems.Add(read.dienThoai);
                    lsvKhachhang.Items[i].SubItems.Add(read.email);
                    lsvKhachhang.Items[i].SubItems.Add(read.taiKhoan.ToString());

            }

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (lsvKhachhang.SelectedItems.Count <= 0)              
                return;

            int id = int.Parse(txtidKH.Text.ToString());
            if ( checkField())
            {
                kh = BizKhachHang.getKhachHangByID(id);
                kh.hoTen = txtHoTen.Text;
                kh.laQuanTri = chkAdmin.Checked;
                kh.laUserXau = chkBUser.Checked;
                kh.diaChi = txtDiachi.Text;
                kh.dienThoai = txtSoDT.Text;
                kh.email = txtEmail.Text;
                kh.taiKhoan = double.Parse(txtTaiKhoan.Text.ToString());
                kh.updateKhachHang();
                show_KH();
                
            }
        }

        private void lsvKhachhang_Click(object sender, EventArgs e)
        {
            if (lsvKhachhang.SelectedItems.Count <= 0)
                return;


            txtidKH.Text = lsvKhachhang.SelectedItems[0].SubItems[0].Text;
            txtHoTen.Text = lsvKhachhang.SelectedItems[0].SubItems[1].Text;
            txtTenDangNhap.Text = lsvKhachhang.SelectedItems[0].SubItems[2].Text;
            if (lsvKhachhang.SelectedItems[0].SubItems[3].Text == "Admin")
            {
                chkAdmin.Checked = true;
            }
            else
            {
                chkAdmin.Checked = false;
            }
            if (lsvKhachhang.SelectedItems[0].SubItems[4].Text == "Bad")
            {
                chkBUser.Checked = true;
            }
            else
            {
                chkBUser.Checked = false;
            }
            txtDiachi.Text = lsvKhachhang.SelectedItems[0].SubItems[5].Text;
            txtSoDT.Text = lsvKhachhang.SelectedItems[0].SubItems[6].Text;
            txtEmail.Text = lsvKhachhang.SelectedItems[0].SubItems[7].Text;
            txtTaiKhoan.Text = lsvKhachhang.SelectedItems[0].SubItems[8].Text;

            groupBoxThongTin.Enabled = true;
        }

        private bool checkField()
        {
            if (txtDiachi.Text.Trim() == "" || txtEmail.Text.Trim() == "" 
                || txtHoTen.Text.Trim() == "" || txtSoDT.Text.Trim() == "" 
                || txtTaiKhoan.Text.Trim() == "")
            {
                MessageBox.Show("Dữ liệu không được để trống!");
                return false;
            }
            return true;
        }

        private void txtSoDT_KeyPress(object sender, KeyPressEventArgs e)
        {
            frmMain.KoNhapSo(sender, e);
        }

        private void txtTaiKhoan_KeyPress(object sender, KeyPressEventArgs e)
        {
            frmMain.KoNhapSo(sender, e);
        }

        private void txtSoDT_KeyDown(object sender, KeyEventArgs e)
        {
            frmMain.KTChiSo(sender, e, txtSoDT, loi);
        }

        private void txtTaiKhoan_KeyDown(object sender, KeyEventArgs e)
        {
            frmMain.KTChiSo(sender, e, txtTaiKhoan, loi);
        }

        private void txtTim_KeyUp(object sender, KeyEventArgs e)
        {
            tim(txtTim.Text);
        }

        private void tim(string giatri)
        {
            lsvKhachhang.Items.Clear();
            kh = new BizKhachHang();
            List<BizKhachHang> lst = BizKhachHang.getListLikeKhachHangByName(giatri);
            if (lst == null)
            {
                lst = BizKhachHang.getListLikeKhachHangByName(giatri);
            }
            if (lst != null)
            {
                foreach (BizKhachHang read in lst)
                {
                    int i = lsvKhachhang.Items.Count;
                    lsvKhachhang.Items.Add(read.idUser.ToString());
                    lsvKhachhang.Items[i].SubItems.Add(read.hoTen);
                    lsvKhachhang.Items[i].SubItems.Add(read.tenDangNhap);
                    if (read.laQuanTri.ToString() == "True")
                    {
                        lsvKhachhang.Items[i].SubItems.Add("Admin");
                    }
                    else
                    {
                        lsvKhachhang.Items[i].SubItems.Add("");
                    }
                    if (read.laUserXau.ToString() == "True")
                    {
                        lsvKhachhang.Items[i].SubItems.Add("Bad");
                    }
                    else
                    {
                        lsvKhachhang.Items[i].SubItems.Add("Good");
                    }
                    lsvKhachhang.Items[i].SubItems.Add(read.diaChi);
                    lsvKhachhang.Items[i].SubItems.Add(read.dienThoai);
                    lsvKhachhang.Items[i].SubItems.Add(read.email);
                    lsvKhachhang.Items[i].SubItems.Add(read.taiKhoan.ToString());

                }
            }
        }

    }
}
